﻿package net.nicoptere.media.video.tracker3d 
{
	import deng.fzip.FZip;
	import flash.events.Event;
	import flash.display.Sprite;
	import net.nicoptere.media.video.tracker3d.Scene;
	
	/**
	 * @author Nicolas Barradeau
	 */
	public class Tracker3D extends Sprite
	{
		static public var FPS:int = 30;
		static public var FOV:Number = 69.98;
		static public var FIX_CAMERA:Boolean = true;
		
		static public var viewportWidth:int = 640;
		static public var viewportHeight:int = 360;
		
		static public var metadataSceneId:String = '';
		static public var debug:Boolean = true;
		
		private var currentScene:uint = 0;
		private var scenes:Array;
		
		/**
		 * 
		 * @param	FOV
		 * @param	FPS
		 */
		public function Tracker3D( FOV:uint, FPS:uint = 30, FIX_CAMERA:Boolean = true ) 
		{
			super();
			
			Tracker3D.FOV = FOV;
			Tracker3D.FPS = FPS;
			Tracker3D.FIX_CAMERA = FIX_CAMERA;
			
		}
		/**
		 * les 3 arrays doivent avoir la même longueur
		 * @param	metaIds 	array de Strings indiquant le nom de chaque scene dans la balise Meta
		 * @param	textures 	array 2d de displayObjects a utiliser comme textures des objets 3D
		 * @param	models		url du model
		 */
		public function init( metaIds:Array, textures:Array, models:Array ):void
		{
			
			
			var i:int;
			var s:String;
			var scene:Scene;
			
			scenes = [];
			for ( i = 0; i < metaIds.length; i++ )
			{
				
				s = metaIds[ i ];
				scene = new Scene( textures[ i ], models[ i ] );
				scenes[ s ] = scene;
				scene.next = ( scenes[ i - 1 ] == null ) ? null : scenes[ i - 1 ];
				addChild( scene );
				
			}
			
			//lance le chargement
			var id:int = ( metaIds.length - 1 < 0 ) ? 0 : ( metaIds.length - 1 ); 
			scene = scenes[  metaIds[ id ] ];
			scene.load();
			scene.addEventListener( Event.COMPLETE, onLoadCompleteHandler );
			
		}
		
		protected function onLoadCompleteHandler( e:Event ):void 
		{
			var scene:Scene = ( e.target as Scene );
			scene.removeEventListener( Event.COMPLETE, onLoadCompleteHandler );
			
			// si il reste une scene à charger, charge la scene suivante
			if ( scene.next != null )
			{
				scene.next.load();
				scene.next.addEventListener( Event.COMPLETE, onLoadCompleteHandler );
			}
			else
			{
				dispatchEvent( new Event( Event.COMPLETE ) );
			}
			
		}
		
		public function gotoAndStop( scene:String, frame:uint ):void
		{
			scenes[ scene ].gotoAndStop( frame ); 
		}
		
	}

}